package com.yupi.springbootinit.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.constant.CommonConstant;
import com.yupi.springbootinit.model.dto.chart.ChartQueryRequest;
import com.yupi.springbootinit.model.dto.chart.ChartQueryRequest;
import com.yupi.springbootinit.model.entity.Chart;
import com.yupi.springbootinit.model.entity.Chart;
import com.yupi.springbootinit.service.ChartService;
import com.yupi.springbootinit.mapper.ChartMapper;
import com.yupi.springbootinit.utils.SqlUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author LX-MF
* @description 针对表【chart(图表信息表)】的数据库操作Service实现
* @createDate 2024-08-27 22:36:10
*/
@Service
public class  ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
    implements ChartService{

    @Resource
    ChartMapper chartMapper;

    @Override
    public QueryWrapper<Chart> getQueryWrapper(ChartQueryRequest chartQueryRequest) {
        QueryWrapper<Chart> queryWrapper = new QueryWrapper<>();
        if (chartQueryRequest == null) {
            return queryWrapper;
        }
        //获取字段参数
        Long id = chartQueryRequest.getId();
        String goal = chartQueryRequest.getGoal();
        String charType = chartQueryRequest.getCharType();
        Long userId = chartQueryRequest.getUserId();
        String sortField = chartQueryRequest.getSortField();
        String sortOrder = chartQueryRequest.getSortOrder();
        String name = chartQueryRequest.getName();
        int current = chartQueryRequest.getCurrent();
        int pageSize = chartQueryRequest.getPageSize();
        //添加查询条件
        queryWrapper.eq(id != null && id>0,"id",id);
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);
        queryWrapper.eq(StringUtils.isNotBlank(goal),"goal",goal);
        queryWrapper.eq(StringUtils.isNotBlank(charType),"charType",charType);
        queryWrapper.eq("isDelete",false);
        queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "userId", userId);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    @Override
    public void createTable(Long chartId, List<String> headerList) {
        String tableName = "user_chart_data_" + chartId;
        StringBuilder stringBuilder = new StringBuilder();
        int n = headerList.size();
        for (int i = 0; i < n; i++) {
            stringBuilder.append("col_").append(i+1).append(" VARCHAR(100) NULL COMMENT ").append("'").append(headerList.get(i)).append("'");
            if(i != n-1) stringBuilder.append(",");
        }
        String createSql = stringBuilder.toString();
        chartMapper.createTable(tableName,createSql);
    }

    @Override
    public void insertTable(Long chartId, List<String> values) {
        String tableName = "user_chart_data_" + chartId;
        chartMapper.insertTable(tableName,values);
    }


}




